let register_url = "http://www.rushmc.top/api/register";
let login_url = "http://www.rushmc.top/api/login";

//注册和登录的ajax内部有点复杂，就没封装了
//获取注册和登录按钮
// let re_sure = document.getElementById('re_sure');
let re_username = document.getElementById('re_username');
let re_password = document.getElementById('re_password');
let re_name = document.getElementById('re_name');
let re_phone = document.getElementById('re_phone');
// let lg_sure = document.getElementById('lg_sure');
let lg_username = document.getElementById('lg_username');
let lg_password = document.getElementById('lg_password');
let remember = document.querySelector('.remember');


var re_sure_request = function () {
    console.log('进入函数');
    //转换为表单的形式
    let data = new FormData();
    //在data中加入参数，形成表单
    data.append("username", re_username.value);
    data.append("password", re_password.value);
    data.append("name", re_name.value);
    data.append("phone", re_phone.value);

    //建立请求对象
    let xmlhttp = new XMLHttpRequest();
    //向服务器发送请求
    xmlhttp.open("post", register_url, true);
    //发送数据
    xmlhttp.send(data);
    //当服务器响应时触发
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            //返回的数据是个字符串，需要调用json.parse方法把他转成js对象
            let res = JSON.parse(xmlhttp.responseText);
            console.log(res.msg);
            if (res.msg === '账号已存在') {
                alert.innerHTML = "账号已存在";
                alert_jump();
                setTimeout(function () {
                    alert.style.display = 'none';
                }, 2000);
            } else {
                alert.innerHTML = "注册成功";
                alert_jump();
                setTimeout(function () {
                    alert.style.display = 'none';
                }, 2000);
            };
        };
    };
};


var lg_sure_request = function () {
    console.log('requestok');
    //记住账号密码
    var remember_checked = document.querySelector('.remember').checked;
    //转换为表单的形式
    let data = new FormData();
    //在data中加入参数，形成表单
    data.append("username", lg_username.value);
    data.append("password", lg_password.value);

    //建立请求对象
    let lg_xmlhttp = new XMLHttpRequest();
    //向服务器发送请求
    lg_xmlhttp.open("post", login_url, true);
    //发送数据
    lg_xmlhttp.send(data);
    //当服务器响应时触发
    lg_xmlhttp.onreadystatechange=function() {
    if (lg_xmlhttp.readyState == 4 && lg_xmlhttp.status == 200) {
        //返回的数据是个字符串，需要调用json.parse方法把他转成js对象
        let res = JSON.parse(lg_xmlhttp.responseText);
        if (res.msg === '成功') {
            alert.innerHTML = "登录成功";
            alert_jump();
            setTimeout(function () {
                alert.style.display = 'none';
            }, 2000);
            if (remember.checked) {
                console.log('true');
                window.localStorage.setItem('username', lg_username.value);
                window.localStorage.setItem('password', lg_password.value);
                var local_username = window.localStorage.getItem('username');
                console.log(local_username);
                lg_change(res.data);
            } else {
                window.localStorage.removeItem('username');
                window.localStorage.removeItem('password');
            }
        } else {
            alert.innerHTML = "账号或密码错误";
            alert_jump();
            setTimeout(function () {
                alert.style.display = 'none';
            }, 2000);
        }
	}
    }
}

//封装的ajax
var ajax_request = function (url,method, ajax_data) {
    //建立请求对象
    let http = new XMLHttpRequest();
    //向服务器发送请求
    http.open(method, url, true);
    //发送数据
    http.send(ajax_data);
    //当服务器响应时触发
    http.onreadystatechange=function() {
    if (http.readyState == 4 && http.status == 200) {
        //返回的数据是个字符串，需要调用json.parse方法把他转成js对象
        let res = JSON.parse(http.responseText);
        console.log("request success!");
        //判断是所有用户动态的请求
        if (method === "get") {
            if (ajax_data == undefined) {
                console.log(res.data);
                createDom(res.data);
            } else {
                console.log(res.data);
                create_my_Dom(res.data);
            }
        } else {
        //判断是上传头像发送的请求
        var photo = ajax_data.get('avatar');
        if (photo === null) {
            console.log("请求不可修改头像")
        } else {
            change_photo(res.data);
        }
        //判断是修改密码发送的请求
        var password = ajax_data.get('newPassword');
        console.log(password);
        if (password === null) {
            console.log("请求不可修改密码");
        }
        }
	}
    }
}